﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommonLibrary.DataModel.Basic;
using Bukovics.Workflow.Hosting;
using System.Workflow.Runtime.Hosting;
//using SharedWorkflows;
using System.Workflow.Activities;
using System.Data;
using ServiceApplication.Service.WorkFlow;
using CommonLibrary.Wf.InterFace;
using CommonLibrary.Wf.Common;

namespace OAclient
{
    public class MyWorkHelpr
    {
        public const string WorkflowRuntimeManager = "WorkflowRuntimeManager";
        public const string WorkflowStaticTemplate = "WorkflowStaticTemplate";
        public const string UserID = "UserID";
        public const string UserEntity = "UserEntity";

        public static UserEntity mySelf;
        public static WorkflowRuntimeManager _workflowManager;
        public static WorkflowPersistenceService _persistence;
        public static BillServer _billServer;
        public static Dictionary<Guid, Workflow> _workflows
            = new Dictionary<Guid, Workflow>();
        public static Workflow _selectedWorkflow;

        //public static DataTable GetAllWorkflowTable()
        //{
        //    //ReadOnlyCollection<WorkflowInstance> os = _workflowManager.WorkflowRuntime.GetLoadedWorkflows();
        //    String sql = "select task_id,uniqueid,fqrcode from wf_task where state='未处理' and user_id=" + MyWorkHelpr.mySelf.duty_id;
        //    String[][] tmpdata4task = CommonLibrary.DB.DBExecuteUtil.querySqlArray(sql);
        //    if (tmpdata4task == null) return null;

        //    DataTable dt = new DataTable("报销单");
        //    dt.Columns.Add("流程号", typeof(Guid));
        //    dt.Columns.Add("报销号", typeof(string));
        //    dt.Columns.Add("报销人ID", typeof(string));
        //    dt.Columns.Add("报销人", typeof(string));
        //    dt.Columns.Add("报销金额", typeof(decimal));
        //    dt.Columns.Add("当前流程步骤", typeof(string));
        //    dt.Columns.Add("财务最后批示", typeof(string));
        //    dt.Columns.Add("老板最后批示", typeof(string));
        //    dt.Columns.Add("出纳最后批示", typeof(string));
        //    dt.Columns.Add("最后批示", typeof(string));
        //    dt.Columns.Add("task_id", typeof(string));

        //    foreach (String[] o in tmpdata4task)
        //    {
        //        string curState = GetCurrentState(new Guid(o[1]));
        //        string sql2 = "select billid,userid,username,money,fremark,bremark,premark,lastremark from billmodel" +
        //            " where  instanceid='" + o[1] + "'";
        //        string[][] tmpdata = CommonLibrary.DB.DBExecuteUtil.querySqlArray(sql2);
        //        if (tmpdata != null)
        //        {
        //            BillModel BM = new BillModel();
        //            BM.BillId = tmpdata[0][0];
        //            BM.UserID = int.Parse(tmpdata[0][1]);
        //            BM.UserName = tmpdata[0][2];
        //            BM.Money = decimal.Parse(tmpdata[0][3]);
        //            BM.Fremark = tmpdata[0][4];
        //            BM.Bremark = tmpdata[0][5];
        //            BM.Premark = tmpdata[0][6];
        //            BM.LastRemark = tmpdata[0][7];
        //            dt.Rows.Add(o[1], BM.BillId, o[2], BM.UserName, BM.Money, curState, BM.Fremark, BM.Bremark, BM.Premark, BM.LastRemark, o[0]);
        //        }
        //        else
        //        {
        //            DataRow dr = dt.NewRow();
        //            dr["流程号"] = o[1];
        //            dr["当前状态"] = curState;
        //            dt.Rows.Add(dr);
        //        }
        //    }
        //    return dt;
        //}

        public static string GetCurrentState(Guid instanceId)
        {
            StateMachineWorkflowInstance stateInstance =
                new StateMachineWorkflowInstance(_workflowManager.WorkflowRuntime, instanceId);
            return stateInstance.CurrentStateName;
        }
    }

    /// <summary>
    /// Used for display of workflow status in a DataGridView
    /// </summary>
    public class Workflow
    {
        public Guid InstanceId { get; set; }
        public String StatusMessage { get; set; }
        public Boolean IsCompleted { get; set; }
    }
}
